// 字符串处理

export const upperFirstLetter = (input: string): string => {
    if (!input) return '';
    return input.charAt(0).toUpperCase() + input.slice(1);
};

type RandConfig = { [key: string]: number };

export const generateRandomString = (config: RandConfig): string => {
    const totalWeight = Object.keys(config).reduce((sum, key) => sum + config[key], 0);
    const randomValue = Math.random() * totalWeight;

    // 累加权重，直到随机数小于当前累加权重
    let cumulativeWeight = 0;
    for (const key in config) {
        cumulativeWeight += config[key];
        if (randomValue < cumulativeWeight) {
            return key;
        }
    }
    throw Error('no such rand config (never happen)');
};
